package com.tony.firewall;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;

import com.tony.firewall.adapter.BlockListAdapter;
import com.tony.firewall.data.PreferencesConfig;
import com.tony.firewall.R;

public class EditBlockActivity extends BaseActivity {
	public static final String BLOCK_TYPE = "block_type";
	private String mType = PreferencesConfig.CONFIG_BLACK_LIST;

	private ListView mListView;
	private View mEditLayout;
	private Button mConfirm, mCancel;

	private BlockListAdapter mAdapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_message_list);

		mEditLayout = findViewById(R.id.ll_select_mode_layout);
		mConfirm = (Button) findViewById(R.id.btn_select_all);
		mConfirm.setOnClickListener(mClickListener);
		mConfirm.setText(R.string.btn_save);
		mCancel = (Button) findViewById(R.id.btn_delete);
		mCancel.setOnClickListener(mClickListener);
		mCancel.setText(R.string.btn_cancel);

		mListView = (ListView) findViewById(R.id.lv_message_list);
		mListView.setDividerHeight(0);
		View footerView = getLayoutInflater().inflate(R.layout.layout_add_block, null);
		mListView.addFooterView(footerView);

		initData();
	}

	private void initData() {
		mType = getIntent().getStringExtra(BLOCK_TYPE);
		mAdapter = new BlockListAdapter(this, mType);
		mListView.setAdapter(mAdapter);
		mAdapter.setOnClickListener(mClickListener);
		mAdapter.setTextWatcher(mTextWatcher);
		if (mType.equals(PreferencesConfig.CONFIG_BLACK_LIST)) {
			getActionBar().setTitle(R.string.setting_edit_block_address);
		}
		else {
			getActionBar().setTitle(R.string.setting_edit_filter_keywords);
		}
	}

	private OnClickListener mClickListener = new OnClickListener() {
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
				case R.id.img_delete_item:
					if (v.getTag() != null) {
						mEditLayout.setVisibility(View.VISIBLE);
						mAdapter.removeChild((Integer) v.getTag());
					}
					break;
				case R.id.btn_select_all:
					PreferencesConfig.editBlockList(EditBlockActivity.this, mType, mAdapter.getList());
				case R.id.btn_delete:
					finish();
					break;
			}
		}
	};

	private CharSequence mNewChar;
	private TextWatcher mTextWatcher = new TextWatcher() {
		@Override
		public void onTextChanged(CharSequence s, int start, int before, int count) {
		}

		@Override
		public void beforeTextChanged(CharSequence s, int start, int count, int after) {
			mNewChar = s;
		}

		@Override
		public void afterTextChanged(Editable s) {
			if (mNewChar != null && !mNewChar.toString().equals(s.toString())) {
				mEditLayout.setVisibility(View.VISIBLE);
			}
		}
	};

	@Override
	public void handleDataEvent(int what, Object obj) {
		// TODO Auto-generated method stub

	}

	@Override
	public void registerListener() {
		// TODO Auto-generated method stub

	}

	@Override
	public void unregisterListener() {
		// TODO Auto-generated method stub

	}

}
